package cn.xiaopengstack.domain.activity.service.trialcal.node;

import cn.xiaopengstack.domain.activity.model.entity.MarketProductEntity;
import cn.xiaopengstack.domain.activity.model.entity.TrialCalculateEntity;
import cn.xiaopengstack.domain.activity.model.valobj.GroupBuyActivityDiscountVO;
import cn.xiaopengstack.domain.activity.model.valobj.SkuVO;
import cn.xiaopengstack.domain.activity.service.trialcal.AbstractGroupBuyMarketSupport;
import cn.xiaopengstack.domain.activity.service.trialcal.factory.DefaultActivityStrategyFactory;
import cn.xiaopengstack.types.framework.strategy.tree.StrategyHandler;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;

/**
 * @author jiangyangang
 */
@Component
@Slf4j
public class EndNode extends AbstractGroupBuyMarketSupport {


    @Override
    protected TrialCalculateEntity doApply(MarketProductEntity param, DefaultActivityStrategyFactory.DynamicContext dynamicContext) throws Exception {
        log.info("拼团商品查询试算服务-EndNode userId:{} requestParameter:{}", param.getUserId(), JSONObject.toJSONString(param));

        GroupBuyActivityDiscountVO discountVO = dynamicContext.getGroupBuyActivityDiscountVO();
        SkuVO skuVO = dynamicContext.getSkuVO();
        BigDecimal payPrice = dynamicContext.getPayPrice();
        return TrialCalculateEntity.builder()
                .endTime(discountVO.getGroupBuyTeamEndTime())
                .startTime(discountVO.getStartTime())
                .targetCount(discountVO.getTarget())
                .goodsId(skuVO.getGoodsId())
                .originalPrice(skuVO.getOriginalPrice())
                .goodsName(skuVO.getGoodsName())
                .deductionPrice(dynamicContext.getDeductionPrice())
                .isEnable(false)
                .isVisible(false)
                .activityId(discountVO.getActivityId())
                .payPrice(payPrice)
                .build();

    }

    @Override
    public StrategyHandler<MarketProductEntity, DefaultActivityStrategyFactory.DynamicContext, TrialCalculateEntity> get(MarketProductEntity param, DefaultActivityStrategyFactory.DynamicContext dynamicContext) throws Exception {
        return defaultStrategyHandler;
    }
}
